home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SliderPanel.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  251 lines

  1. /*
  2.  * @(#)SliderPanel.java    1.12 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.event.*;
  23. import com.sun.java.swing.border.*;
  24. import com.sun.java.accessibility.*;
  25.  
  26. import java.awt.Panel;
  27. import java.awt.Color;
  28. import java.awt.BorderLayout;
  29. import java.awt.GridLayout;
  30. import java.awt.Font;
  31.  
  32. class SliderListener implements ChangeListener {
  33.     JLabel tf;
  34.     public SliderListener(JLabel f) {
  35.         tf = f;
  36.     }
  37.     
  38.     public void stateChanged(ChangeEvent e) {
  39.         JSlider s1 = (JSlider)e.getSource();
  40.         tf.setText("Slider Value: " + s1.getValue());
  41.     }
  42. }
  43.  
  44. /*
  45.  * @version 1.12 02/02/98
  46.  * @author Dave Kloba
  47.  * @author Peter Korn (accessibility support)
  48.  */
  49. public class SliderPanel extends JPanel      {
  50.     // The Frame
  51.     SwingSet swing;
  52.  
  53.     public SliderPanel(SwingSet swing)    {
  54.         JSlider s;
  55.     JPanel hp;
  56.     JPanel vp;
  57.     GridLayout g;
  58.     JPanel tp;
  59.     JLabel tf;
  60.     ChangeListener listener;
  61.  
  62.         this.swing = swing;
  63.  
  64.     this.setLayout(new BorderLayout());
  65.  
  66.     tf = new JLabel("Slider Value: " );
  67.     this.add(tf, BorderLayout.SOUTH);
  68.     
  69.     tp = new JPanel();
  70.     g = new GridLayout(1, 2);
  71.     g.setHgap(5);
  72.     g.setVgap(5);
  73.     tp.setLayout(g);
  74.     this.add(tp, BorderLayout.CENTER);
  75.         
  76.     listener = new SliderListener(tf);
  77.  
  78.     hp = new JPanel();
  79.     hp.setLayout(new BoxLayout(hp, BoxLayout.Y_AXIS));
  80.     hp.setBorder(new TitledBorder( 
  81.             swing.lightLoweredBorder, 
  82.             "Horizontal",
  83.             TitledBorder.LEFT,
  84.             TitledBorder.ABOVE_TOP));
  85.     tp.add(hp);
  86.  
  87.     vp = new JPanel();
  88.     vp.setLayout(new BoxLayout(vp, BoxLayout.X_AXIS));
  89.     vp.setBorder(new TitledBorder( 
  90.             swing.lightLoweredBorder, 
  91.             "Vertical",
  92.             TitledBorder.LEFT,
  93.             TitledBorder.ABOVE_TOP));
  94.     tp.add(vp);
  95.  
  96.     // Horizontal Slider 1
  97.     JPanel p = new JPanel();
  98.     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  99.     p.setBorder(new TitledBorder("Plain"));
  100.     s = new JSlider(JSlider.HORIZONTAL, -10, 100, 20);
  101.     s.getAccessibleContext().setAccessibleName("Plain");
  102.     s.getAccessibleContext().setAccessibleDescription("A plain slider");
  103.     s.addChangeListener(listener);
  104.  
  105.     p.add(Box.createRigidArea(swing.vpad5));
  106.     p.add(s);
  107.     p.add(Box.createRigidArea(swing.vpad5));
  108.     hp.add(p);
  109.     hp.add(Box.createRigidArea(swing.vpad10));
  110.  
  111.     // Horizontal Slider 2
  112.     p = new JPanel();
  113.     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  114.     p.setBorder(new TitledBorder("Major Ticks"));
  115.     s = new JSlider(JSlider.HORIZONTAL, 100, 1000, 400);
  116.     s.setPaintTicks(true);
  117.     s.setMajorTickSpacing(100);
  118.     s.getAccessibleContext().setAccessibleName("Major Ticks");
  119.     s.getAccessibleContext().setAccessibleDescription("A slider showing major tick marks");
  120.     s.addChangeListener(listener);
  121.  
  122.     p.add(Box.createRigidArea(swing.vpad5));
  123.     p.add(s);
  124.     p.add(Box.createRigidArea(swing.vpad5));
  125.     hp.add(p);
  126.     hp.add(Box.createRigidArea(swing.vpad10));
  127.  
  128.     // Horizontal Slider 3
  129.     p = new JPanel();
  130.     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  131.     p.setBorder(new TitledBorder("Minor Ticks, Snap-to-ticks and Labels"));
  132.     s = new JSlider(JSlider.HORIZONTAL, 0, 11, 6);
  133.  
  134.     s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );
  135.  
  136.     s.setPaintTicks(true);
  137.     s.setMajorTickSpacing(5);
  138.     s.setMinorTickSpacing(1);
  139.  
  140.     s.setPaintLabels( true );
  141.     s.setSnapToTicks( true );
  142.  
  143.     s.getLabelTable().put( new Integer( 11 ), new JLabel( "11", JLabel.CENTER ) );
  144.     s.setLabelTable( s.getLabelTable() );
  145.  
  146.     s.getAccessibleContext().setAccessibleName("Minor Ticks");
  147.     s.getAccessibleContext().setAccessibleDescription("A slider showing major and minor tick marks, with slider action snapping to tick marks, with some ticks visibly labeled");
  148.  
  149.     s.addChangeListener(listener);
  150.  
  151.     p.add(Box.createRigidArea(swing.vpad5));
  152.     p.add(s);
  153.     p.add(Box.createRigidArea(swing.vpad5));
  154.     hp.add(p);
  155.     hp.add(Box.createRigidArea(swing.vpad10));
  156.  
  157.     // Horizontal Slider 4
  158.     p = new JPanel();
  159.     p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
  160.     p.setBorder(new TitledBorder("Disabled"));
  161.     s = new JSlider(JSlider.HORIZONTAL, 0, 100, 80);
  162.     s.setPaintTicks(true);
  163.     s.setMajorTickSpacing(20);
  164.     s.setMinorTickSpacing(5);
  165.     s.setEnabled(false);
  166.     s.getAccessibleContext().setAccessibleName("Disabled");
  167.     s.getAccessibleContext().setAccessibleDescription("A slider showing major and minor tick marks that is not enabled (cannot be manipulated)");
  168.     s.addChangeListener(listener);
  169.  
  170.     p.add(Box.createRigidArea(swing.vpad5));
  171.     p.add(s);
  172.     p.add(Box.createRigidArea(swing.vpad5));
  173.     hp.add(p);
  174.     
  175. //////////////////////////////////////////////////////////////////////////////
  176.  
  177.     // Vertical Slider 1
  178.     p = new JPanel();
  179.     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  180.     p.setBorder(new TitledBorder("Plain"));
  181.     s = new JSlider(JSlider.VERTICAL, -10, 100, 20);
  182.     s.getAccessibleContext().setAccessibleName("Plain");
  183.     s.getAccessibleContext().setAccessibleDescription("A plain slider");
  184.     s.addChangeListener(listener);
  185.     p.add(Box.createRigidArea(swing.hpad10));
  186.     p.add(s);
  187.     p.add(Box.createRigidArea(swing.hpad10));
  188.     vp.add(p);
  189.     vp.add(Box.createRigidArea(swing.hpad5));
  190.  
  191.     // Vertical Slider 2
  192.     p = new JPanel();
  193.     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  194.     p.setBorder(new TitledBorder("Major Ticks"));
  195.     s = new JSlider(JSlider.VERTICAL, 100, 1000, 400);
  196.  
  197.     s.putClientProperty( "JSlider.isFilled", Boolean.TRUE );
  198.  
  199.     s.setPaintTicks(true);
  200.     s.setMajorTickSpacing(100);
  201.     s.getAccessibleContext().setAccessibleName("Major Ticks");
  202.     s.getAccessibleContext().setAccessibleDescription("A slider showing major tick marks");
  203.     s.addChangeListener(listener);
  204.     p.add(Box.createRigidArea(swing.hpad25));
  205.     p.add(s);
  206.     p.add(Box.createRigidArea(swing.hpad25));
  207.     vp.add(p);
  208.     vp.add(Box.createRigidArea(swing.hpad5));
  209.  
  210.     // Vertical Slider 3
  211.     p = new JPanel();
  212.     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  213.     p.setBorder(new TitledBorder("Minor Ticks"));
  214.     s = new JSlider(JSlider.VERTICAL, 0, 100, 60);
  215.     s.setPaintTicks(true);
  216.     s.setMajorTickSpacing(20);
  217.     s.setMinorTickSpacing(5);
  218.  
  219.     s.setPaintLabels( true );
  220.  
  221.     s.getAccessibleContext().setAccessibleName("Minor Ticks");
  222.     s.getAccessibleContext().setAccessibleDescription("A slider showing major and minor tick marks, with slider action snapping to tick marks, with some ticks visibly labeled");
  223.  
  224.     s.addChangeListener(listener);
  225.     p.add(Box.createRigidArea(swing.hpad10));
  226.     p.add(s);
  227.     p.add(Box.createRigidArea(swing.hpad10));
  228.     vp.add(p);
  229.     vp.add(Box.createRigidArea(swing.hpad5));
  230.  
  231.     // Vertical Slider 4
  232.     p = new JPanel();
  233.     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
  234.     p.setBorder(new TitledBorder("Disabled"));
  235.     s = new JSlider(JSlider.VERTICAL, 0, 100, 80);
  236.     s.setPaintTicks(true);
  237.     s.setMajorTickSpacing(20);
  238.     s.setMinorTickSpacing(5);
  239.     s.setEnabled(false);
  240.     s.getAccessibleContext().setAccessibleName("Disabled");
  241.     s.getAccessibleContext().setAccessibleDescription("A slider showing major and minor tick marks that is not enabled (cannot be manipulated)");
  242.     s.addChangeListener(listener);
  243.     p.add(Box.createRigidArea(swing.hpad20));
  244.     p.add(s);
  245.     p.add(Box.createRigidArea(swing.hpad20));
  246.     vp.add(p);
  247.     }
  248. }
  249.  
  250.  
  251.